﻿using M.ShopMachine.Domain.Dto;
using M.ShopMachine.Domain;
using M.ShopMachine.Read.WebApi.Application.Command.DeviceCommand;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using M.ShopMachine.Read.WebApi.Application.Command.PatrolCommand;
using System.Collections.Generic;
using M.ShopMachine.Read.WebApi.Application.Command.MaintainCommand;

namespace M.ShopMachine.Read.WebApi.Controllers
{
    /// <summary>
    /// 机床api
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        private readonly IMediator mediator;

        public DeviceController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 机床-列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Device>>> GetTableDeviceAsync([FromQuery] GetTableDeviceCommand command)
        {
            ApiResult<List<Device>> result = new ApiResult<List<Device>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 机床-查看-巡查信息列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Patrol>>> GetTablePatrolAsync([FromQuery]GetTablePatrolCommand command)
        {
            ApiResult<List<Patrol>> result = new ApiResult<List<Patrol>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 机床-查看-保养信息列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Maintain>>> GetTableMaintainAsync([FromQuery]GetTableMaintainCommand command)
        {
            ApiResult<List<Maintain>> result = new ApiResult<List<Maintain>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 车间添加获取机床列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Device>>> GetShopDeviceAsync([FromQuery]GetShopDeviceCommand command)
        {
            ApiResult<List<Device>> result = new ApiResult<List<Device>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 车间查看获取机床列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Device>>> GetShopMachineAsync([FromQuery]GetShopMachineCommand command)
        {
            ApiResult<List<Device>> result = new ApiResult<List<Device>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code=500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 机床-查看反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Device>> GetFormDeviceAsync([FromQuery]GetFormDeviceCommand command)
        {
            ApiResult<Device> result = new ApiResult<Device>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
